// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Bomb Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.startPos.x
private alias object.value2 : object.startPos.y
private alias object.value3 : object.fusePos

private alias 0 : BOMB_WALK
private alias 1 : BOMB_IDLE
private alias 2 : BOMB_EXPLODE

private alias 0 : BOMB_ANI_WALK
private alias 1 : BOMB_ANI_IDLE
private alias 2 : BOMB_ANI_EXPLODING


// ========================
// Function Declarations
// ========================

reserve function Bomb_DebugDraw
reserve function Bomb_DebugSpawn


// ========================
// Function Definitions
// ========================

private function Bomb_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(8, FX_FLIP, object.xpos, object.ypos)
end function


private function Bomb_DebugSpawn
	CreateTempObject(TypeName[Bomb], 0, object.xpos, object.ypos)
	object[tempObjectPos].startPos.x = object[tempObjectPos].xpos
	object[tempObjectPos].startPos.y = object[tempObjectPos].ypos
	object[tempObjectPos].xvel = -0x1000
	
	temp0 = object.direction
	object[tempObjectPos].direction = temp0
	temp0 &= FLIP_X
	if temp0 == FLIP_X
		FlipSign(object[tempObjectPos].xvel)
	end if

	object[tempObjectPos].timer = 0x600
	object[tempObjectPos].animation = BOMB_ANI_WALK
	object[tempObjectPos].frame = 0
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case BOMB_WALK
		object.priority = PRIORITY_ACTIVE
		object.timer--
		if object.timer == 0
			object.timer = 180
			object.animation = BOMB_ANI_IDLE
			object.animationTimer = 0
			object.frame = 4
			object.state = BOMB_IDLE
		else
			object.xpos += object.xvel
		end if
		break

	case BOMB_IDLE
		object.timer--
		if object.timer == 0
			object.timer = 0x600
			object.animation = BOMB_ANI_WALK
			object.animationTimer = 0
			object.frame = 0
			FlipSign(object.xvel)
			object.direction ^= FLIP_X
			object.state = BOMB_WALK
		end if
		break

	case BOMB_EXPLODE
		object.fusePos += 0x1000
		object.timer--
		if object.timer == 0
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, object.xpos, object.ypos)
			CreateTempObject(TypeName[Explosion], 0, object.xpos, object.ypos)

			CreateTempObject(TypeName[Bomb Shrapnel], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = -0x20000
			object[tempObjectPos].yvel = -0x30000

			CreateTempObject(TypeName[Bomb Shrapnel], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = -0x10000
			object[tempObjectPos].yvel = -0x20000

			CreateTempObject(TypeName[Bomb Shrapnel], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = 0x20000
			object[tempObjectPos].yvel = -0x30000

			CreateTempObject(TypeName[Bomb Shrapnel], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = 0x10000
			object[tempObjectPos].yvel = -0x20000

			PlaySfx(SfxName[Explosion], false)
		end if
		break
		
	end switch

	if object.outOfBounds == true
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.startPos.x
		object.ypos = object.startPos.y
		if object.outOfBounds == true
			object.xvel = -0x1000
			temp0 = object.propertyValue
			object.direction = temp0
			temp0 &= FLIP_X
			if temp0 == FLIP_X
				FlipSign(object.xvel)
			end if
			object.timer = 0x600
			object.animation = BOMB_ANI_WALK
			object.frame = 0
			object.priority = PRIORITY_BOUNDS
			object.state = BOMB_WALK
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.state != BOMB_EXPLODE
			BoxCollisionTest(C_TOUCH, object.entityPos, -96, -96, 96, 96, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				object.animation = BOMB_ANI_EXPLODING
				object.frame = 6
				object.timer = 144
				object.fusePos = 0
				object.state = BOMB_EXPLODE
			end if
		end if

		BoxCollisionTest(C_TOUCH, object.entityPos, -12, -18, 12, 18, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			CallFunction(Player_Hit)
		end if
	next

	switch object.animation
	case BOMB_ANI_WALK
		if object.animationTimer == 20
			object.animationTimer = 0
			object.frame++
			object.frame &= 3
		end if
		object.animationTimer++
		break

	case BOMB_ANI_IDLE
	case BOMB_ANI_EXPLODING
		if object.animationTimer == 20
			object.animationTimer = 0
			object.frame ^= 1
		end if
		object.animationTimer++
		break
		
	end switch
end event


event ObjectDraw
	if object.state == BOMB_EXPLODE
		temp0 = object.timer
		temp0 >>= 2
		temp0 &= 1
		temp0 += 8
		temp1 = object.ypos
		
		// Get the Y-Flip bit
		GetBit(temp2, object.direction, 1)
		if temp2 == FLIP_NONE
			// FLIP_NONE or FLIP_X (aka upright)
			temp1 += object.fusePos
		else
			// FLIP_Y or FLIP_XY (aka on a roof)
			temp1 -= object.fusePos
		end if
		
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, temp1)
	end if
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	// Load sprites depending on current scene folder

	CheckCurrentStageFolder("SAND")
	if checkResult == true
		LoadSpriteSheet("SSZ/Objects2.gif")
		SpriteFrame(-10, -21, 20, 37, 53, 131)
		SpriteFrame(-10, -22, 20, 38, 28, 131)
		SpriteFrame(-10, -21, 20, 37, 3, 131)
		SpriteFrame(-10, -22, 20, 38, 53, 92)
		SpriteFrame(-11, -21, 23, 37, 2, 92)
		SpriteFrame(-11, -21, 23, 37, 27, 92)
		SpriteFrame(-11, -9, 23, 25, 2, 170)
		SpriteFrame(-11, -9, 23, 25, 27, 170)
		SpriteFrame(-3, -23, 6, 14, 51, 170)
		SpriteFrame(-4, -25, 8, 16, 58, 170)
	end if

	CheckCurrentStageFolder("SAND")
	if checkResult == true
		LoadSpriteSheet("SSZ/Objects2.gif")
		SpriteFrame(-10, -21, 20, 37, 52, 40)
		SpriteFrame(-10, -22, 20, 38, 27, 40)
		SpriteFrame(-10, -21, 20, 37, 2, 40)
		SpriteFrame(-10, -22, 20, 38, 52, 1)
		SpriteFrame(-11, -21, 23, 37, 1, 1)
		SpriteFrame(-11, -21, 23, 37, 26, 1)
		SpriteFrame(-11, -9, 23, 25, 1, 79)
		SpriteFrame(-11, -9, 23, 25, 26, 79)
		SpriteFrame(-3, -23, 6, 14, 50, 79)
		SpriteFrame(-4, -25, 8, 16, 57, 79)
	end if

	// Unused stuff from the "MBZ" Zone
	CheckCurrentStageFolder("MBZ")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-10, -21, 20, 37, 53, 328)
		SpriteFrame(-10, -22, 20, 38, 28, 328)
		SpriteFrame(-10, -21, 20, 37, 3, 328)
		SpriteFrame(-10, -22, 20, 38, 53, 289)
		SpriteFrame(-11, -21, 23, 37, 2, 289)
		SpriteFrame(-11, -21, 23, 37, 27, 289)
		SpriteFrame(-11, -9, 23, 25, 2, 367)
		SpriteFrame(-11, -9, 23, 25, 27, 367)
		SpriteFrame(-3, -23, 6, 14, 51, 367)
		SpriteFrame(-4, -25, 8, 16, 58, 367)
	end if

	foreach (TypeName[Bomb], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		object[arrayPos0].xvel = -0x1000
		temp0 = object[arrayPos0].propertyValue
		object[arrayPos0].direction = temp0
		temp0 &= FLIP_X
		if temp0 == FLIP_X
			FlipSign(object[arrayPos0].xvel)
		end if
		object[arrayPos0].timer = 0x600
		object[arrayPos0].animation = BOMB_ANI_WALK
		object[arrayPos0].frame = 0
	next
	
	// Add the object to the debug item list
	SetTableValue(TypeName[Bomb], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Bomb_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Bomb_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // direction
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // direction
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	object.direction = object.propertyValue
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	// Make sure we have a fall-back, in case the current folder isn't actually a valid one
	temp0 = false

	CheckCurrentStageFolder("Zone06")
	temp0 |= checkResult
	if checkResult == true
		LoadSpriteSheet("SSZ/Objects2.gif")
		SpriteFrame(-10, -21, 20, 37, 52, 40)
		SpriteFrame(-10, -22, 20, 38, 27, 40)
		SpriteFrame(-10, -21, 20, 37, 2, 40)
		SpriteFrame(-10, -22, 20, 38, 52, 1)
		SpriteFrame(-11, -21, 23, 37, 1, 1)
		SpriteFrame(-11, -21, 23, 37, 26, 1)
		SpriteFrame(-11, -9, 23, 25, 1, 79)
		SpriteFrame(-11, -9, 23, 25, 26, 79)
		SpriteFrame(-3, -23, 6, 14, 50, 79)
		SpriteFrame(-4, -25, 8, 16, 57, 79)
	end if

	CheckCurrentStageFolder("Zone07")
	temp0 |= checkResult
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-10, -21, 20, 37, 53, 328)
		SpriteFrame(-10, -22, 20, 38, 28, 328)
		SpriteFrame(-10, -21, 20, 37, 3, 328)
		SpriteFrame(-10, -22, 20, 38, 53, 289)
		SpriteFrame(-11, -21, 23, 37, 2, 289)
		SpriteFrame(-11, -21, 23, 37, 27, 289)
		SpriteFrame(-11, -9, 23, 25, 2, 367)
		SpriteFrame(-11, -9, 23, 25, 27, 367)
		SpriteFrame(-3, -23, 6, 14, 51, 367)
		SpriteFrame(-4, -25, 8, 16, 58, 367)
	end if

	if temp0 == false
		// We use SSZ's sprites as the fallback, "Zone05" (SSZ's folder) will come here too
		LoadSpriteSheet("MBZ/Objects2.gif")
		SpriteFrame(-10, -21, 20, 37, 53, 131)
		SpriteFrame(-10, -22, 20, 38, 28, 131)
		SpriteFrame(-10, -21, 20, 37, 3, 131)
		SpriteFrame(-10, -22, 20, 38, 53, 92)
		SpriteFrame(-11, -21, 23, 37, 2, 92)
		SpriteFrame(-11, -21, 23, 37, 27, 92)
		SpriteFrame(-11, -9, 23, 25, 2, 170)
		SpriteFrame(-11, -9, 23, 25, 27, 170)
		SpriteFrame(-3, -23, 6, 14, 51, 170)
		SpriteFrame(-4, -25, 8, 16, 58, 170)
	end if

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
	AddEnumVariable("Left (Flip Y)", 2)
	AddEnumVariable("Right (Flip Y)", 3)
end event
